// pages/moddetail/moddetail.js
import util from '../../../../utils/util.js'
var app=getApp()
var host=app.globalData.host
function checky(y)
{
	if((y%4==0&&y%100!=0)||y%400==0)
	{
		return true;
	}
}
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		p:{},
		index:0,
		offices:["神经内科"],
		name:'NAN',
		num:'',
		sex:'',
		age:-1,
		phoneNum:'',
		keshi:'请选择',
		regDate:new Date(),
		treatCnt:0,
		intervalTime:0,
		status:true,
		illness:'',
		datamissing: new Array(10),
		debugg:0,
		mzh:'',
		fre:0,
		nextdate:'',
		zd:'',
		date:util.formatDate(new Date()),
		r1:"false",
		r2:"false",
		patientId:'',
		bh:'',
		zyh:''
	},
	confirmName: function(e)
	{
		this.setData({
			name : e.detail.value
		})
	},
	confirmSex1: function()
	{
		this.setData({
			sex:'男'
		})
	},
	confirmSex2: function()
	{
		this.setData({
			sex:'女'
		})
	},
	confirmNum: function(e)
	{
		this.setData({
			num : e.detail.value
		})
	},
	confirmAge: function(e)
	{
		this.setData({
			age : e.detail.value
		})
	},
	confirmPhonenum: function(e)
	{
		this.setData({
			phoneNum : e.detail.value
		})
	},
	confirmKeshi: function(e)
	{
		this.setData({
			keshi : e.detail.value
		})
	},
	bindkeshiChange: function(e)
	{
		console.log(e);
		this.data.index=e.detail.value
		this.setData({
			keshi: this.data.offices[this.data.index]
		})
	},
	confirmdate: function(e)
	{
		this.setData({
			date : (e.detail.value)
		})
	},
	confirmMzh: function(e)
	{
		this.setData({
			mzh : (e.detail.value)
		})
	},
	confirmBh: function(e)
	{
		this.setData({
			bh : (e.detail.value)
		})
	},
	confirmZyh: function(e)
	{
		this.setData({
			zyh : (e.detail.value)
		})
	},
	confirmZd: function(e)
	{
		this.setData({
			zd : (e.detail.value)
		})
	},
	confirmfre: function(e)
	{
		this.setData({
			fre : parseInt(e.detail.value)
		})
		var datee=this.data.p.patientLast;
        var y = parseInt(datee.substring(0,4));
        var m = parseInt(datee.substring(5,7));
        var d = parseInt(datee.substring(8,10));
		var freq=parseInt(e.detail.value);
		var dates=[0,31,28,31,30,31,30,31,31,30,31,30,31];
        for(var i=0;i<freq;i++)
        {
            if(d==dates[m])
            {
				if(m!=12)
				{
                	m++;
                	d=1;
				}
				else
				{
					m=1;
					d=1;
					y++;
				}
            }
            else
            {
                d++;
            }
        }
		y=y+"";
		m=m+"";
		d=d+"";
		this.setData({
			nextdate:y+'-'+m+'-'+d
		})
	},
	check()
	{
		console.log(typeof(this.data.fre))
		if(this.data.age==''||this.data.age==null)
			return false
		if(this.data.phoneNum==''||this.data.phoneNum==null)
			return false
		if(this.data.mzh==''||this.data.mzh==null)
			return false
		if(this.data.zd==''||this.data.zd==null)
			return false
		if(this.data.fre>0)
		{
			return true
		}
		else
		{
			wx.showToast({
			  title: '频率应<=7',
			  icon:'error'
			})
			this.setData({
				f:1
			})
			return false
		}
    },
    precover()
    {
        let that=this;
        console.log(that.data.p.patientId)
        wx.request({
          url: host+'/doctor/recover',
          method:'PUT',
			header:{
                "token":app.globalData.userInfo_doc.msg,
                'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
            },
            data:{
                patientPhone:that.data.p.patientPhone
            },
            success(res)
            {
                console.log()
                console.log(res);
                wx.showToast({
                  title: '恢复成功',
                  icon:'success'
                })
                wx.navigateBack()
            },
        })
    },
	infoMod:function()
	{
		this.setData({
			f:0
		})
		if(!this.check())
		{
			console.log("内容不对")
			if(this.data.f!=1)
				wx.showToast({
				title: '修改不能为空',
				icon:'error'
				})
			return;
		}
		this.setData({
			p:
			{
				patientAge: this.data.age,
				patientCtime: this.data.date,
				patientGender: this.data.sex,
				patientId: this.data.p.patientId,
				patientMzh: this.data.mzh,
				patientName: this.data.name,
				patientNext: this.data.nextdate,
				patientNum: this.data.p.patientNum,
				patientOffice: this.data.offices,
				patientOpenid: this.data.p.patientOpenid,
				patientPhone: this.data.phoneNum,
				patientPl: this.data.fre,
				patientState: this.data.p.patientState,
				patientZd: this.data.zd,
			}
		})
		let that=this;
		wx.request({
		  	url: host+'/doctor/patient',
		  	method:'PUT',
			header:{
				"token":app.globalData.userInfo_doc.msg,
			},
			data: 
			{
				/*
				"patientAge": this.data.age,
				"patientCtime": this.data.date,
				"patientGender": this.data.sex,
				"patientId": this.data.p.patientId,
				"patientMzh": this.data.mzh,
				"patientName": this.data.name,
				"patientNext": this.data.nextdate,
				"patientNum": 0,
				"patientOffice": this.data.offices,
				"patientOpenid": this.data.p.patientOpenid,
				"patientPhone": this.data.phoneNum,
				"patientPl": this.data.fre,
				"patientState": this.data.p.patientState,
				"patientZd": this.data.zd,*/
				"patientAge": this.data.age,
				"patientCtime": this.data.date,
				"patientGender": this.data.sex,
				"patientId": this.data.p.patientId,
				"patientMzh": this.data.mzh,
				"patientName": this.data.name,
				"patientNext": this.data.nextdate,
				"patientNum": this.data.p.patientNum,
				"patientOffice": this.data.keshi,
				"patientOpenid": this.data.p.patientOpenid,
				"patientPhone": this.data.phoneNum,
				"patientPl": this.data.fre,
				"patientState": this.data.p.patientState,
				"patientZd": this.data.zd,
				"patientBh": this.data.bh,
				"patientZyh": this.data.zyh
			},
			success(res)
			{
				console.log(res)
				if(res.statusCode==200)
				{
					if(res.data.code==10000)
					{
						wx.navigateBack()
						wx.showToast({
							title: '修改成功'
						})
					}
					else
					{
						if(res.data.msg=="该手机号已存在,更新失败")
						{
							wx.showToast({
							title: '手机号已存在',
							icon:'error'
							})
						}
						else
						{
							wx.showToast({
								title: res.data.msg,
								icon:'error'
								})
						}
					}
				}
				else
				{
					wx.showToast({
					  title: '出现错误',
					  icon:'error'
					})
				}
			},
			fail(res)
			{
				console.log(res)
				wx.showToast({
				  title: '修改失败',
					icon:"error"
				})
			}
		})
	},
	pend:function()
	{
		let that=this;
		wx.request({
		  	url: host+'/doctor/end',
			method:'PUT',
			header:  
			{
				"token":app.globalData.userInfo_doc.msg,
				'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
			},
			data:
			{
				"patientId":that.data.p.patientId,
				"type":1
			},
			success(res)
			{
				console.log(res);
				wx.navigateBack();
				wx.showToast({
				  title: '结束随访',
				});
			}
		})
	},
	plost:function()
	{
		let that=this;
		wx.request({
		  	url: host+'/doctor/end',
			method:'PUT',
			header:  
			{
				"token":app.globalData.userInfo_doc.msg,
				'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
			},
			data:
			{
				"patientId":that.data.p.patientId,
				"type":2
			},
			success(res)
			{
				console.log(res);
				wx.navigateBack();
				wx.showToast({
				  title: '已设为病人失访',
				});
			}
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		this.setData({
			p:wx.getStorageSync('modThisPatient')
		})
		this.setData(
		{
			age:		this.data.p.patientAge,
			date:		this.data.p.patientCtime,
			sex:		this.data.p.patientGender,
			mzh:		this.data.p.patientMzh,
			name:		this.data.p.patientName,
			nexrdate:	this.data.p.patientNext,
			keshi:		this.data.p.patientOffice,
			phoneNum:	this.data.p.patientPhone,
			fre:		this.data.p.patientPl,
			zd:			this.data.p.patientZd,
			last:		this.data.p.patientLast,
			bh:			this.data.p.patientBh
		})
		console.log(this.data.p.patientGender+"")
		console.log(this.data.p.patientGender+""=="男")
		if(this.data.p.patientGender+""=="男")
		{
			this.setData({
				r1:"true"
			})
		}
		else
		{
			this.setData({
				r2:"true"
			})
		}
		this.setData({
			nextdate:this.data.p.patientNext
		})
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})